// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { AdManager } from "../ads/AdManager";
import { AdManager_KS } from "../ads/AdManager_KS";
import { AdManager_WX } from "../ads/AdManager_WX";
import { AdManager_ZJ } from "../ads/AdManager_ZJ";
import { EVENT_NAME_ENUM, GAME_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { TimeTools } from "../common/TimeTools";
import { Tools } from "../common/Tools";
import { AudioManager1 } from "../manager/AudioManager1";
import { EventManager } from "../manager/EventManager";
import { LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";
const tt = window['tt'];
const { ccclass, property } = cc._decorator;
const SIGN_ADDFREENUM = [1, 2, -1, 1, 1, 1, 2];
const SIGN_ADDFREENUM_NAO_DONG = [0, 0, 1, 0, 0, 0, 1];
@ccclass
export default class SignUI extends cc.Component {


    @property({ type: cc.Label, displayName: "剩余免广告卡数量" })
    lbFreeCnt: cc.Label = null;

    @property({ type: [cc.Node], displayName: "天数节点" })
    dayNodes: Array<cc.Node> = [];

    @property({ type: cc.Label, displayName: "添加桌面" })
    lbAdd: cc.Label = null;

    @property({ type: cc.Node, displayName: "添加桌面选择框" })
    select: cc.Node = null;


    private isAdd: boolean = false;
    private isSelect: boolean = true;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.updateStatus();
        this.node.getChildByName("mask").on("click", () => {
            this.clickBtnClose();
        });
        this.node.getChildByName("bg").getChildByName("lbAdd").active = false;
        this.node.getChildByName("bg").getChildByName("select").active = false;
        if (AdManager.gameType != GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN) {
            for (let i = 0; i < this.dayNodes.length; i++) {
                let dayNode = this.dayNodes[i];
                dayNode.getChildByName("ka").active = false;
                dayNode.getChildByName("ka_ks").active = true;
                if (SIGN_ADDFREENUM[i] > 0) {
                    dayNode.getChildByName("cnt").active = true;
                    dayNode.getChildByName("cnt").getComponent(cc.Label).string = "x" + SIGN_ADDFREENUM[i];
                }
            }
        }
        if (AdManager.releaseType == RELEASE_TYPE_ENUM.ks) {
            this.node.getChildByName("bg").getChildByName("lbAdd").active = true;
            this.node.getChildByName("bg").getChildByName("select").active = true;
            /**展示添加桌面即为还没有添加 */
            AdManager.isShowAddShortcutBtn((active: boolean) => {
                this.isAdd = !active;
            });
        }
    }

    clickAddShortcut() {
        this.isSelect = !this.isSelect;
    }

    updateFreeCnt() {
        let freeAdNum = Tools.getStorage("freeAdNum");
        if (freeAdNum === null) {
            freeAdNum = 0;
        }
        this.lbFreeCnt.string = "免广告卡剩余X" + freeAdNum;
    }

    updateStatus() {
        let data: any[] = Tools.getStorage("signData", true);
        if (data === null || data.length == 0) {
            this.dayNodes[0].getChildByName("status_2").active = true;
            this.dayNodes[0].getChildByName("status_1").active = false;
        } else {
            let day = data.length;
            let lastTime = data[data.length - 1];
            let day1 = TimeTools._ins.getTimeData(lastTime).day;
            let day2 = TimeTools._ins.getTimeData().day;
            if (day >= 7 && day1 != day2) { //第八天重新开签到
                // EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_GIFT_BTN);
                Tools.setStorage("signData", [], true);
                this.updateStatus();
                return;
            }
            if (day < 7 && day1 != day2) {
                this.dayNodes[day].getChildByName("status_2").active = true;
                this.dayNodes[day].getChildByName("status_1").active = false;
            } else {
                this.dayNodes[day - 1].getChildByName("status_2").active = false;
                this.dayNodes[day - 1].getChildByName("status_1").active = true;
            }
            for (let i = 0; i < data.length; i++) {
                this.dayNodes[i].getChildByName("zhezhao").active = this.dayNodes[i].getChildByName("yilingqu").active = true;
            }
        }
        this.updateFreeCnt();
    }

    clickSign() {
        let data: any[] = Tools.getStorage("signData", true);
        if (data) {
            let lastTime = data[data.length - 1];
            let day1 = TimeTools._ins.getTimeData(lastTime).day;
            let day2 = TimeTools._ins.getTimeData().day;
            if (day1 == day2) {
                console.log("今日已签到");
                UIManager.ins.showToast("今日已签到");
                return;
            }
        }
        if (AdManager.releaseType == RELEASE_TYPE_ENUM.ks) {
            if (!this.isAdd && this.isSelect === false) {
                UIManager.ins.showToast("请勾选添加桌面选项")
                return;
            }
            if (!this.isAdd && this.isSelect) {
                UIManager.ins.showToast("奖励发放中，请勿关闭界面！！！");
                this.scheduleOnce(() => {
                    this.onSign();
                }, 5)
                AdManager.addTable(() => {
                    EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN);
                });
            }
        } else {
            this.onSign();
        }

    }

    onSign() {

        let data: any[] = Tools.getStorage("signData", true);
        if (data === null) {
            let tempData = new Date().getTime()
            Tools.setStorage("signData", [tempData], true);
            let reward = SIGN_ADDFREENUM;
            if (AdManager.gameType == GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN) {
                reward = SIGN_ADDFREENUM_NAO_DONG;
            }
            if (AdManager.releaseType == RELEASE_TYPE_ENUM.wechat) {
                AdManager_WX.ins.sign(reward[0]);
            } else if (AdManager.releaseType == RELEASE_TYPE_ENUM.ks) {
                AdManager_KS.ins.sign(reward[0]);
            }
            else {
                AdManager_ZJ.ins.sign(reward[0]);
            }
            this.updateStatus();
            return;
        }
        let lastTime = data[data.length - 1];
        let day1 = TimeTools._ins.getTimeData(lastTime).day;
        let day2 = TimeTools._ins.getTimeData().day;
        let copyData = data.concat();
        if (day1 == day2) {
            console.log("今日已签到");
            UIManager.ins.showToast("今日已签到");
            // tt.showToast({
            //     title: "今日已签到",
            //     icon: "none",
            // });
        } else {
            copyData.push(new Date().getTime());
            copyData.sort((a, b) => {
                return a - b;
            })
            Tools.setStorage("signData", copyData, true);

            let reward = SIGN_ADDFREENUM;
            if (AdManager.gameType == GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN) {
                reward = SIGN_ADDFREENUM_NAO_DONG;
            }
            if (AdManager.gameType != GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN) {
                if (reward[data.length] === -1) {
                    LevelManager.ins.unlockAllVideoNum = 0;
                    LevelManager.ins.watchAllVideo2();
                    LevelManager.ins.watchAllVideo2();
                    LevelManager.ins.watchAllVideo2();
                    if (LevelManager.ins.isUnlockAll()) {
                        EventManager.ins.emit(EVENT_NAME_ENUM.UNLOCK_ALL_LEVEL);
                        UIManager.ins.close();
                    }
                } else {
                    if (AdManager.releaseType == RELEASE_TYPE_ENUM.ks) {
                        AdManager_KS.ins.sign(reward[data.length]);
                    } else if (AdManager.releaseType == RELEASE_TYPE_ENUM.wechat) {
                        AdManager_WX.ins.sign(reward[data.length]);
                    } else {
                        AdManager_ZJ.ins.sign(reward[data.length]);
                    }
                }
                this.updateStatus();
                return;
            }
            if (AdManager.releaseType == RELEASE_TYPE_ENUM.wechat) {
                AdManager_WX.ins.sign(reward[data.length]);
            } else {
                AdManager_ZJ.ins.sign(reward[data.length]);
            }
            this.updateStatus();
        }
    }

    clickBtnClose() {
        AudioManager1.playClick();
        UIManager.ins.close();
    }

    // update (dt) {}
}
